package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.CacheDoubleStaticUtils;
import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.MovieAdapter;
import flc.ast.bean.MyCollectBean;
import flc.ast.databinding.ActivityMovieBinding;
import flc.ast.utils.AppSpUtils;
import stark.common.api.StkApi;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;

public class MovieActivity extends BaseAc<ActivityMovieBinding> {

    protected MovieAdapter movieAdapter;
    private int page = 1;
    private int refreshTime = 200;

    @Override
    protected int onCreate() {
        return R.layout.activity_movie;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(this, mDataBinding.event1Container);
        mDataBinding.ivMovieBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        mDataBinding.rvMovieList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        movieAdapter = new MovieAdapter();
        mDataBinding.rvMovieList.setAdapter(movieAdapter);
        movieAdapter.addChildClickViewIds(R.id.ivRecommendItemCollect);
        movieAdapter.setOnItemChildClickListener(this);
        movieAdapter.setOnItemClickListener(this);
        movieAdapter.notifyDataSetChanged();
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        StkResBean bean=movieAdapter.getItem(position);
        DetailActivity.detail=bean;
        startActivity(DetailActivity.class);
    }

    @Override
    public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
        StkResBean bean = movieAdapter.getItem(position);
        switch (view.getId()) {
            case R.id.ivRecommendItemCollect:
                List<MyCollectBean> listSp = AppSpUtils.getCollectList();
                if (movieAdapter.getItem(position).isSelected()) {
                    for (MyCollectBean myCollectBean : listSp) {
                        if (myCollectBean.getPath().equals(bean.getThumbUrl())) {
                            listSp.remove(myCollectBean);
                            break;
                        }
                    }
                    AppSpUtils.saveCollectList(listSp);
                    bean.setSelected(false);
                } else {
                    List<MyCollectBean> listSave = new ArrayList<>();
                    try {
                        Gson gson = new Gson();
                        String json = gson.toJson(bean.getExtra());
                        JSONObject jsonObject = new JSONObject(json);
                        String releaseDate = jsonObject.getString("releaseDate");
                        int index = releaseDate.indexOf("(");
                        String dateStr = releaseDate.substring(0, index);
                        Date date = TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy-MM-dd"));
                        if (date==null){
                            date=TimeUtils.string2Date(dateStr, new SimpleDateFormat("yyyy"));
                        }
                        listSave.add(new MyCollectBean(bean.getName(), bean.getThumbUrl(), date.getTime()));
                        if (listSp != null && listSp.size() != 0) {
                            listSp.addAll(listSave);
                            AppSpUtils.saveCollectList(listSp);
                        } else {
                            AppSpUtils.saveCollectList(listSave);
                        }
                        bean.setSelected(true);
                    } catch (JSONException e) {
                        throw new RuntimeException(e);
                    }
                }
                movieAdapter.notifyItemChanged(position);
                break;
        }
    }

    @Override
    protected void initData() {
        getData();
        mDataBinding.refreshMovieItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshMovieItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshMovieItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData();
                mDataBinding.refreshMovieItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData();
                mDataBinding.refreshMovieItem.finishRefresh(refreshTime);
            }
        });
    }

    private void getData() {
        //获取数据
        StkResApi.getTagResourceList(null, AppConfig.MOVIE_DATA, StkApi.createParamMap(0, 10), false,
                new IReqRetCallback<List<StkResBean>>() {
                    @Override
                    public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                        if (success) {
                            for (StkResBean stkResBean : result) {
                                List<MyCollectBean> listSp = AppSpUtils.getCollectList();
                                if (listSp != null && listSp.size() != 0) {
                                    for (MyCollectBean myCollectBean : listSp) {
                                        if (myCollectBean.getPath().equals(stkResBean.getThumbUrl())) {
                                            stkResBean.setSelected(true);
                                            break;
                                        }
                                    }
                                } else {
                                    break;
                                }
                            }
                            if (page == 1) {
                                movieAdapter.setList(result);
                            } else {
                                movieAdapter.addData(result);
                            }
                        }
                    }
                });
    }

}